/**
 * 
 */
package it.rainbowbreeze.polygen.logic;

import it.rainbowbreeze.polygen.R;
import it.rainbowbreeze.polygen.common.AppEnv;
import it.rainbowbreeze.polygen.util.TestHelper;
import android.content.Context;
import android.test.AndroidTestCase;
import android.text.TextUtils;

/**
 * @author rainbowbreeze
 *
 */
public class MessageParserTest extends AndroidTestCase {
    private MessageParser mMessageParser;
    
    
    public MessageParserTest() {
    }
    
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        Context context = getContext();
        assertNotNull("Context is null", context);
        TestHelper.init(context);
        
        mMessageParser = AppEnv.i(context).getMessageParser();
        assertNotNull(mMessageParser);
    }

    public void testParseNullMessage() {
        assertNull("Wrong result", mMessageParser.parse(null));
    }
    
    public void testParse_Message1() {
        testParse_Message(
                TestHelper.getContentFromRawResource(getContext(), R.raw.testmessage1),
                TestHelper.getContentFromRawResource(getContext(), R.raw.testmessage1_parsed));
    }

    public void testParse_Message2() {
        testParse_Message(
                TestHelper.getContentFromRawResource(getContext(), R.raw.testmessage2),
                TestHelper.getContentFromRawResource(getContext(), R.raw.testmessage2_parsed));
    }

    public void testParse_Message3() {
        testParse_Message(
                TestHelper.getContentFromRawResource(getContext(), R.raw.testmessage3),
                TestHelper.getContentFromRawResource(getContext(), R.raw.testmessage3_parsed));
    }
    
    
    public void testParse_Message(byte[] rawTestMessage, byte[] rawExpectedMessage) {
        assertNotNull("Wrong raw resource", rawTestMessage);
        String sourceMessage = new String(rawTestMessage);
        assertFalse("Wrong source message", TextUtils.isEmpty(sourceMessage));
        
        assertNotNull("Wrong raw resource", rawExpectedMessage);
        String expectedMessage = new String(rawExpectedMessage);
        assertFalse("Wrong source message", TextUtils.isEmpty(expectedMessage));
        
        String actual = mMessageParser.parse(sourceMessage);
        assertNotNull("Wrong result", actual);
        assertEquals("Wrong result", expectedMessage, actual);
    }


}
